package sample.road;

import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

public class LineSegment {
	
	@Override
	public String toString() {
		return ToStringBuilder.reflectionToString(this, ToStringStyle.DEFAULT_STYLE);
	}
	
	private static Pattern pattern = Pattern.compile(
			"NAME:([RB])-(\\d+) X1:(\\S*) Y1:(\\S*) X2:(\\S*) Y2:(\\S*) SRC:R-(\\d+) DST:R-(\\d+)"
			);
	
	public LineSegment(String inputString) {
		Matcher matcher = pattern.matcher(inputString);
		if (matcher.find()) {
			setType(matcher.group(1));
			setNo(matcher.group(2));
			setX1(matcher.group(3));
			setY1(matcher.group(4));
			setX2(matcher.group(5));
			setY2(matcher.group(6));
			setSrcRoad(matcher.group(7));
			setDstRoad(matcher.group(8));
			
		} else {
			throw new InstantiationError("input pattern is illegal. input : " + inputString);
		}
	}
	
	private String type;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getNo() {
		return no;
	}
	public void setNo(String no) {
		this.no = no;
	}
	public String getX1() {
		return x1;
	}
	public void setX1(String x1) {
		this.x1 = x1;
	}
	public String getY1() {
		return y1;
	}
	public void setY1(String y1) {
		this.y1 = y1;
	}
	public String getX2() {
		return x2;
	}
	public void setX2(String x2) {
		this.x2 = x2;
	}
	public String getY2() {
		return y2;
	}
	public void setY2(String y2) {
		this.y2 = y2;
	}
	public String getSrcRoad() {
		return srcRoad;
	}
	public void setSrcRoad(String srcRoad) {
		this.srcRoad = srcRoad;
	}
	public String getDstRoad() {
		return dstRoad;
	}
	public void setDstRoad(String dstRoad) {
		this.dstRoad = dstRoad;
	}
	private String no;
	private String x1;
	private String y1;
	private String x2;
	private String y2;
	private String srcRoad;
	private String dstRoad;

	public Node getStartNode(Set<Node> nodeSet) {
		for (Node node : nodeSet) {
			if (node.equals(new Node(x1, y1))) {
				return node;
			}
		}
		return null;
	}
	public Node getEndNode(Set<Node> nodeSet) {
		for (Node node : nodeSet) {
			if (node.equals(new Node(x2, y2))) {
				return node;
			}
		}
		return null;
	}

}
